#include "EditorNivel.h"
#include <string>

EditorNivel::EditorNivel(BaseObjectType* cobject,
		const Glib::RefPtr<Gtk::Builder>& refGlade) :
		Gtk::Window(cobject), builder(refGlade) {
	//tomamos los widgets de glade
	builder->get_widget("wintablero", wintablero);
	builder->get_widget("spinfila", spinfila);
	builder->get_widget("spincol", spincol);
	builder->get_widget("combocolor", combocolor);
	builder->get_widget("windownew", windownew);
	builder->get_widget("new_menu_item", new_menu_item);
	builder->get_widget("open_menu_item", open_menu_item);
	builder->get_widget("saveas_menu_item", saveas_menu_item);
	builder->get_widget("combotipo", combotipo);
	builder->get_widget("label1", label1);
	builder->get_widget("spinprob", spinprob);
	builder->get_widget("spinnivel", spinnivel);
	builder->get_widget("toggleselect", toggleselect);
	builder->get_widget("spinpuntaje", spinpuntaje);
	//cargadores archivos
	builder->get_widget("filefondo", filefondo);
	builder->get_widget("filechoosersonido", filechoosersonido);
	builder->get_widget("filechooser", filechooser);
	builder->get_widget("filechoosersave", filechoosersave);
	//ventana abrir
	builder->get_widget("buttonchooserabrir", buttonchooserabrir);
	builder->get_widget("buttonchoosercancelar", buttonchoosercancelar);
	//ventana guardar
	builder->get_widget("buttonsave", buttonsave);
	builder->get_widget("buttoncancelarsave", buttoncancelarsave);
	//nueva ventana
	builder->get_widget("buttonaceptar", buttonaceptar);
	builder->get_widget("buttoncancelar", buttoncancelar);

	// asigna la ventana donde se agregara el tablero
	//poner en funcion
	tablero = NULL;
			
	new_menu_item->signal_activate().connect(
			sigc::mem_fun(*this, &EditorNivel::on_new_menu_item_activate));		
			
	open_menu_item->signal_activate().connect(
			sigc::mem_fun(*this, &EditorNivel::on_open_menu_item_activate));			
			
	saveas_menu_item->signal_activate().connect(
			sigc::mem_fun(*this, &EditorNivel::on_saveas_menu_item_activate));	
					
	buttonaceptar->signal_clicked().connect(
			sigc::mem_fun(*this, &EditorNivel::on_buttonaceptar_clicked));
			
	buttoncancelar->signal_clicked().connect(
			sigc::mem_fun(*this, &EditorNivel::on_buttoncancelar_clicked));
			
		buttonchooserabrir->signal_clicked().connect(
			sigc::mem_fun(*this, &EditorNivel::on_buttonchooserabrir_clicked));
			
	buttonchoosercancelar->signal_clicked().connect(
			sigc::mem_fun(*this, &EditorNivel::on_buttonchoosercancelar_clicked));	
			
					buttonsave->signal_clicked().connect(
			sigc::mem_fun(*this, &EditorNivel::on_buttonsave_clicked));
			
	buttoncancelarsave->signal_clicked().connect(
			sigc::mem_fun(*this, &EditorNivel::on_buttoncancelarsave_clicked));			
}


void EditorNivel::on_new_menu_item_activate() {
	windownew->show();
}

void EditorNivel::on_open_menu_item_activate() {
	filechooser->show();
}

void EditorNivel::on_saveas_menu_item_activate() {
std::string base;
std::string numNivel= convertirIntaStr(spinnivel->get_value());
base+="nivel";
base+=numNivel;
base+=".json";

filechoosersave->show();

filechoosersave->set_current_name(base);

}

void EditorNivel::on_buttonsave_clicked() {

if(filechoosersonido->get_filename().size()!=0)
tablero->setDirSonido(getNombreFile(filechoosersonido->get_filename()));	
if(filefondo->get_filename().size()!=0)
tablero->setDirFondo(getNombreFile(filefondo->get_filename()));

tablero->setNivel(spinnivel->get_value());
tablero->setPuntaje(spinpuntaje->get_value());

std::ofstream out(filechoosersave->get_filename().c_str(), std::ofstream::out);
out << tablero->toJson();
out.close();	

filechoosersave->hide();
	
}

void EditorNivel::on_buttoncancelarsave_clicked() {
	filechoosersave->hide();
}


void EditorNivel::on_toggleselect_toggled() {
	if (toggleselect->get_active() == false) {
		tablero->getCeldaSelected()->desactivarSeleccion();
		tablero->setCeldaSelected(NULL);
	}
}



void EditorNivel::on_spinprob_changed() {
	int idTipo = combotipo->get_active_row_number();
	int idColor = combocolor->get_active_row_number();
	unsigned char valor = spinprob->get_value();
	//label1->set_text(convertirIntaStr(valor));

	if (tablero->getCeldaSelected() != NULL) {
		switch (idTipo) {
		case 0:

			switch (idColor) {
			case 0:
				tablero->getCeldaSelected()->getDatos()->setProbButtDefault(valor);
				break;
			case 1:
				tablero->getCeldaSelected()->getDatos()->setProbButtAma(valor);
				break;
			case 2:
				tablero->getCeldaSelected()->getDatos()->setProbButtAzu(valor);
				break;
			case 3:
				tablero->getCeldaSelected()->getDatos()->setProbButtRoj(valor);
				break;
			case 4:
				tablero->getCeldaSelected()->getDatos()->setProbButtVer(valor);
				break;
			case 5:
				tablero->getCeldaSelected()->getDatos()->setProbButtVio(valor);
				break;
			}
			break;
			
		case 1:
			switch (idColor) {
			case 0:
				tablero->getCeldaSelected()->getDatos()->setProbBarDefault(valor);
				break;
			case 1:
				tablero->getCeldaSelected()->getDatos()->setProbBarAma(valor);
				break;
			case 2:
				tablero->getCeldaSelected()->getDatos()->setProbBarAzu(valor);
				break;
			case 3:
				tablero->getCeldaSelected()->getDatos()->setProbBarRoj(valor);
				break;
			case 4:
				tablero->getCeldaSelected()->getDatos()->setProbBarVer(valor);
				break;
			case 5:
				tablero->getCeldaSelected()->getDatos()->setProbBarVio(valor);
				break;
			}
			break;
		case 2:
		
			tablero->getCeldaSelected()->getDatos()->setProbStarDefault(valor);
			break;
		}
	}
}

void EditorNivel::on_combo_changed() {
		tablero->setProbSeleccionada();
}

void EditorNivel::activarControlesEditor() {
	combotipo->signal_changed().connect(
			sigc::mem_fun(*this, &EditorNivel::on_combo_changed));
			
	combocolor->signal_changed().connect(
			sigc::mem_fun(*this, &EditorNivel::on_combo_changed));
			
		spinprob->signal_changed().connect(
			sigc::mem_fun(*this, &EditorNivel::on_spinprob_changed));		
			
	toggleselect->signal_toggled().connect(
			sigc::mem_fun(*this, &EditorNivel::on_toggleselect_toggled));
}


void EditorNivel::on_buttonaceptar_clicked() {

	int fil = spinfila->get_value();
	int col = spincol->get_value();

	tablero = new TableroEditor(builder);
	tablero->generarTablero(fil, col);
	
	windownew->hide();
	
	activarControlesEditor();
}

void EditorNivel::on_buttoncancelar_clicked() {
	windownew->hide();
}

void EditorNivel::on_buttonchooserabrir_clicked() {
	if(filechooser->get_filename().size()!=0){
	filechooser->hide();
	  
	tablero = new TableroEditor(builder);
	
	std::ifstream archivo(filechooser->get_filename().c_str());
	
	Json::Value bookJson;
	archivo >> bookJson;  
	
	tablero->cargarDatosAtablero(bookJson);
	
	activarControlesEditor();
	
	spinpuntaje->set_value(bookJson["puntaje"].asInt());
	
	spinnivel->set_value(bookJson["nivel"].asInt());
	
	}
}

void EditorNivel::on_buttonchoosercancelar_clicked() {
	filechooser->hide();
}

std::string EditorNivel::convertirIntaStr(int entero) {
	std::stringstream sentero;
	sentero << entero;
	std::string mensaje = sentero.str();
	return mensaje;
}

std::string EditorNivel::getNombreFile(std:: string filename) {
const size_t last_slash_idx = filename.find_last_of("\\/");
if (std::string::npos != last_slash_idx)
{
    filename.erase(0, last_slash_idx + 1);
}
return filename;
}

EditorNivel::~EditorNivel(){
delete tablero;
}
